package com.example.erp.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.erp.bus.entity.GoodsType;
import com.example.erp.bus.entity.Supplier;
import com.example.erp.bus.service.SupplierService;
import com.example.erp.bus.vo.GoodstypeVo;
import com.example.erp.bus.vo.SupplierVo;
import com.example.erp.common.utils.DataGridViewResult;
import com.example.erp.common.utils.JSONResult;
import com.example.erp.common.utils.SystemConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LiQiCheng
 * @since 2022-02-19
 */
@RestController
@RequestMapping("/bus/supplier")
public class SupplierController {

    @Resource
    private SupplierService supplierService;

    /**
     * 供应商列表
     *
     * @param supplierVo
     * @return
     */
    @RequestMapping("/supplierlist")
    public DataGridViewResult supplierlist(SupplierVo supplierVo) {
        //创建分页对象
        IPage<Supplier> page = new Page<Supplier>(supplierVo.getPage(), supplierVo.getLimit());
        //创建条件构造器对象
        QueryWrapper<Supplier> queryWrapper = new QueryWrapper<Supplier>();
        //分类名称查询
        queryWrapper.like(StringUtils.isNotBlank(supplierVo.getName()), "name", supplierVo.getName());
        //排序
        queryWrapper.orderByAsc("id");
        //调用查询的方法
        supplierService.page(page, queryWrapper);
        //返回数据
        return new DataGridViewResult(page.getTotal(), page.getRecords());
    }

    /**
     * 添加供应商
     *
     * @param supplier
     * @return
     */
    @RequestMapping("/addSupplier")
    public JSONResult addSupplier(Supplier supplier) {
        if (supplierService.save(supplier)) {
            return SystemConstant.ADD_SUCCESS;
        }
        return SystemConstant.ADD_ERROR;
    }

    /**
     * 修改供应商
     *
     * @param supplier
     * @return
     */
    @RequestMapping("/updateSupplier")
    public JSONResult updateSupplier(Supplier supplier) {
        if (supplierService.updateById(supplier)) {
            return SystemConstant.UPDATE_SUCCESS;
        }
        return SystemConstant.UPDATE_ERROR;
    }

    /**
     * 删除供应商
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteById")
    public JSONResult deleteById(int id) {
        if (supplierService.removeById(id)) {
            return SystemConstant.DELETE_SUCCESS;
        } else {
            return SystemConstant.DELETE_ERROR;
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    public JSONResult delete(String ids) {
        //将字符串拆分成数组
        String[] idsStr = ids.split(",");
        //判断是否删除成功
        if (supplierService.removeByIds(Arrays.asList(idsStr))) {
            return SystemConstant.DELETE_SUCCESS;//删除成功
        }
        //删除失败
        return SystemConstant.DELETE_ERROR;
    }
}

